home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / c++ / cursesf.cc < prev    next >
C/C++ Source or Header  |  2002-10-24  |  12KB  |  428 lines

  1. // * this is for making emacs happy: -*-Mode: C++;-*-
  2. /****************************************************************************
  3.  * Copyright (c) 1998,2001 Free Software Foundation, Inc.                   *
  4.  *                                                                          *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  6.  * copy of this software and associated documentation files (the            *
  7.  * "Software"), to deal in the Software without restriction, including      *
  8.  * without limitation the rights to use, copy, modify, merge, publish,      *
  9.  * distribute, distribute with modifications, sublicense, and/or sell       *
  10.  * copies of the Software, and to permit persons to whom the Software is    *
  11.  * furnished to do so, subject to the following conditions:                 *
  12.  *                                                                          *
  13.  * The above copyright notice and this permission notice shall be included  *
  14.  * in all copies or substantial portions of the Software.                   *
  15.  *                                                                          *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  18.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  19.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  20.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  21.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  22.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  23.  *                                                                          *
  24.  * Except as contained in this notice, the name(s) of the above copyright   *
  25.  * holders shall not be used in advertising or otherwise to promote the     *
  26.  * sale, use or other dealings in this Software without prior written       *
  27.  * authorization.                                                           *
  28.  ****************************************************************************/
  29.  
  30. /****************************************************************************
  31.  *   Author: Juergen Pfeifer, 1997                                          *
  32.  *   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en             *
  33.  ****************************************************************************/
  34.  
  35. #include "internal.h"
  36. #include "cursesf.h"
  37. #include "cursesapp.h"
  38.  
  39. MODULE_ID("$Id: cursesf.cc,v 1.15 2002/07/06 15:47:52 juergen Exp $")
  40.  
  41. NCursesFormField::~NCursesFormField () {
  42.   if (field)
  43.     OnError(::free_field (field));
  44. }
  45.  
  46. /* Construct a FIELD* array from an array of NCursesFormField
  47.  * objects.
  48.  */
  49. FIELD**
  50. NCursesForm::mapFields(NCursesFormField* nfields[]) {
  51.   int fieldCount = 0,lcv;
  52.   FIELD** old_fields;
  53.  
  54.   assert(nfields != 0);
  55.  
  56.   for (lcv=0; nfields[lcv]->field; ++lcv)
  57.     ++fieldCount;
  58.  
  59.   FIELD** fields = new FIELD*[fieldCount + 1];
  60.  
  61.   for (lcv=0;nfields[lcv]->field;++lcv) {
  62.     fields[lcv] = nfields[lcv]->field;
  63.   }
  64.   fields[lcv] = NULL;
  65.  
  66.   my_fields = nfields;
  67.  
  68.   if (form && (old_fields = ::form_fields(form))) {
  69.     ::set_form_fields(form,(FIELD**)0);
  70.     delete[] old_fields;
  71.   }
  72.   return fields;
  73. }
  74.  
  75. void NCursesForm::setDefaultAttributes() {
  76.   NCursesApplication* S = NCursesApplication::getApplication();
  77.  
  78.   int n = count();
  79.   if (n > 0) {
  80.     for(int i=0; i<n; i++) {
  81.       NCursesFormField* f = (*this)[i];
  82.       if ((f->options() & (O_EDIT|O_ACTIVE))==(O_EDIT|O_ACTIVE)) {
  83.     if (S) {
  84.       f->set_foreground(S->foregrounds());
  85.       f->set_background(S->backgrounds());
  86.     }
  87.     f->set_pad_character('_');
  88.       }
  89.       else {
  90.     if (S)
  91.       f->set_background(S->labels());
  92.       }
  93.     }
  94.   }
  95.  
  96.   if (S) {
  97.     bkgd(' '|S->dialog_backgrounds());
  98.     if (sub)
  99.       sub->bkgd(' '|S->dialog_backgrounds());
  100.   }
  101. }
  102.  
  103. void
  104. NCursesForm::InitForm(NCursesFormField* nfields[],
  105.               bool with_frame,
  106.               bool autoDelete_Fields) {
  107.   int mrows, mcols;
  108.  
  109.   keypad(TRUE);
  110.   meta(TRUE);
  111.  
  112.   b_framed = with_frame;
  113.   b_autoDelete = autoDelete_Fields;
  114.  
  115.   form = (FORM*)0;
  116.   form = ::new_form(mapFields(nfields));
  117.   if (!form)
  118.     OnError (E_SYSTEM_ERROR);
  119.  
  120.   UserHook* hook = new UserHook;
  121.   hook->m_user   = NULL;
  122.   hook->m_back   = this;
  123.   hook->m_owner  = form;
  124.   ::set_form_userptr(form,(void*)hook);
  125.  
  126.   ::set_form_init  (form, NCursesForm::frm_init);
  127.   ::set_form_term  (form, NCursesForm::frm_term);
  128.   ::set_field_init (form, NCursesForm::fld_init);
  129.   ::set_field_term (form, NCursesForm::fld_term);
  130.  
  131.   scale(mrows, mcols);
  132.   ::set_form_win(form, w);
  133.  
  134.   if (with_frame) {
  135.     if ((mrows > height()-2) || (mcols > width()-2))
  136.       OnError(E_NO_ROOM);
  137.     sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
  138.     ::set_form_sub(form, sub->w);
  139.     b_sub_owner = TRUE;
  140.   }
  141.   else {
  142.     sub = (NCursesWindow*)0;
  143.     b_sub_owner = FALSE;
  144.   }
  145.   options_on(O_NL_OVERLOAD);
  146.   setDefaultAttributes();
  147. }
  148.  
  149. NCursesForm::~NCursesForm() {
  150.   UserHook* hook = (UserHook*)::form_userptr(form);
  151.   delete hook;
  152.   if (b_sub_owner) {
  153.     delete sub;
  154.     ::set_form_sub(form,(WINDOW *)0);
  155.   }
  156.   if (form) {
  157.     FIELD** fields = ::form_fields(form);
  158.     int cnt = count();
  159.  
  160.     OnError(::set_form_fields(form,(FIELD**)0));
  161.  
  162.     if (b_autoDelete) {
  163.       if (cnt>0) {
  164.     for (int i=0; i <= cnt; i++)
  165.       delete my_fields[i];
  166.       }
  167.       delete[] my_fields;
  168.     }
  169.  
  170.     ::free_form(form);
  171.     // It's essential to do this after free_form()
  172.     delete[] fields;
  173.   }
  174. }
  175.  
  176. void
  177. NCursesForm::setSubWindow(NCursesWindow& nsub) {
  178.   if (!isDescendant(nsub))
  179.     OnError(E_SYSTEM_ERROR);
  180.   else {
  181.     if (b_sub_owner)
  182.       delete sub;
  183.     sub = ⊄
  184.     ::set_form_sub(form,sub->w);
  185.   }
  186. }
  187.  
  188. /* Internal hook functions. They will route the hook
  189.  * calls to virtual methods of the NCursesForm class,
  190.  * so in C++ providing a hook is done simply by
  191.  * implementing a virtual method in a derived class
  192.  */
  193. void
  194. NCursesForm::frm_init(FORM *f) {
  195.   getHook(f)->On_Form_Init();
  196. }
  197.  
  198. void
  199. NCursesForm::frm_term(FORM *f) {
  200.   getHook(f)->On_Form_Termination();
  201. }
  202.  
  203. void
  204. NCursesForm::fld_init(FORM *f) {
  205.   NCursesForm* F = getHook(f);
  206.   F->On_Field_Init (*(F->current_field ()));
  207. }
  208.  
  209. void
  210. NCursesForm::fld_term(FORM *f) {
  211.   NCursesForm* F = getHook(f);
  212.   F->On_Field_Termination (*(F->current_field ()));
  213. }
  214.  
  215. void
  216. NCursesForm::On_Form_Init() {
  217. }
  218.  
  219. void
  220. NCursesForm::On_Form_Termination() {
  221. }
  222.  
  223. void
  224. NCursesForm::On_Field_Init(NCursesFormField& field) {
  225. }
  226.  
  227. void
  228. NCursesForm::On_Field_Termination(NCursesFormField& field) {
  229. }
  230.  
  231. // call the form driver and do basic error checking.
  232. int
  233. NCursesForm::driver (int c) {
  234.   int res = ::form_driver (form, c);
  235.   switch (res) {
  236.   case E_OK:
  237.   case E_REQUEST_DENIED:
  238.   case E_INVALID_FIELD:
  239.   case E_UNKNOWN_COMMAND:
  240.     break;
  241.   default:
  242.     OnError (res);
  243.   }
  244.   return (res);
  245. }
  246.  
  247. void NCursesForm::On_Request_Denied(int c) const {
  248.   ::beep();
  249. }
  250.  
  251. void NCursesForm::On_Invalid_Field(int c) const {
  252.   ::beep();
  253. }
  254.  
  255. void NCursesForm::On_Unknown_Command(int c) const {
  256.   ::beep();
  257. }
  258.  
  259. static const int CMD_QUIT = MAX_COMMAND + 1;
  260.  
  261. NCursesFormField*
  262. NCursesForm::operator()(void) {
  263.   int drvCmnd;
  264.   int err;
  265.   int c;
  266.  
  267.   post();
  268.   show();
  269.   refresh();
  270.  
  271.   while (((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
  272.     switch((err=driver(drvCmnd))) {
  273.     case E_REQUEST_DENIED:
  274.       On_Request_Denied(c);
  275.       break;
  276.     case E_INVALID_FIELD:
  277.       On_Invalid_Field(c);
  278.       break;
  279.     case E_UNKNOWN_COMMAND:
  280.       On_Unknown_Command(c);
  281.       break;
  282.     case E_OK:
  283.       break;
  284.     default:
  285.       OnError(err);
  286.     }
  287.   }
  288.  
  289.   unpost();
  290.   hide();
  291.   refresh();
  292.   return my_fields[::field_index (::current_field (form))];
  293. }
  294.  
  295. // Provide a default key virtualization. Translate the keyboard
  296. // code c into a form request code.
  297. // The default implementation provides a hopefully straightforward
  298. // mapping for the most common keystrokes and form requests.
  299. int
  300. NCursesForm::virtualize(int c) {
  301.   switch(c) {
  302.  
  303.   case KEY_HOME      : return(REQ_FIRST_FIELD);
  304.   case KEY_END       : return(REQ_LAST_FIELD);
  305.  
  306.   case KEY_DOWN      : return(REQ_DOWN_CHAR);
  307.   case KEY_UP        : return(REQ_UP_CHAR);
  308.   case KEY_LEFT      : return(REQ_PREV_CHAR);
  309.   case KEY_RIGHT     : return(REQ_NEXT_CHAR);
  310.  
  311.   case KEY_NPAGE     : return(REQ_NEXT_PAGE);
  312.   case KEY_PPAGE     : return(REQ_PREV_PAGE);
  313.  
  314.   case KEY_BACKSPACE : return(REQ_DEL_PREV);
  315.   case KEY_ENTER     : return(REQ_NEW_LINE);
  316.   case KEY_CLEAR     : return(REQ_CLR_FIELD);
  317.  
  318.   case CTRL('X')     : return(CMD_QUIT);        // eXit
  319.  
  320.   case CTRL('F')     : return(REQ_NEXT_FIELD);  // Forward
  321.   case CTRL('B')     : return(REQ_PREV_FIELD);  // Backward
  322.   case CTRL('L')     : return(REQ_LEFT_FIELD);  // Left
  323.   case CTRL('R')     : return(REQ_RIGHT_FIELD); // Right
  324.   case CTRL('U')     : return(REQ_UP_FIELD);    // Up
  325.   case CTRL('D')     : return(REQ_DOWN_FIELD);  // Down
  326.  
  327.   case CTRL('W')     : return(REQ_NEXT_WORD);
  328.   case CTRL('T')     : return(REQ_PREV_WORD);
  329.  
  330.   case CTRL('A')     : return(REQ_BEG_FIELD);
  331.   case CTRL('E')     : return(REQ_END_FIELD);
  332.  
  333.   case CTRL('I')     : return(REQ_INS_CHAR);
  334.   case CTRL('M')     :
  335.   case CTRL('J')     : return(REQ_NEW_LINE);
  336.   case CTRL('O')     : return(REQ_INS_LINE);
  337.   case CTRL('V')     : return(REQ_DEL_CHAR);
  338.   case CTRL('H')     : return(REQ_DEL_PREV);
  339.   case CTRL('Y')     : return(REQ_DEL_LINE);
  340.   case CTRL('G')     : return(REQ_DEL_WORD);
  341.   case CTRL('K')     : return(REQ_CLR_EOF);
  342.  
  343.   case CTRL('N')     : return(REQ_NEXT_CHOICE);
  344.   case CTRL('P')     : return(REQ_PREV_CHOICE);
  345.  
  346.   default:
  347.     return(c);
  348.   }
  349. }
  350. //
  351. // -------------------------------------------------------------------------
  352. // User Defined Fieldtypes
  353. // -------------------------------------------------------------------------
  354. //
  355. bool UserDefinedFieldType::fcheck(FIELD *f, const void *u) {
  356.   NCursesFormField* F = (NCursesFormField*)u;
  357.   assert(F != 0);
  358.   UserDefinedFieldType* udf = (UserDefinedFieldType*)(F->fieldtype());
  359.   assert(udf != 0);
  360.   return udf->field_check(*F);
  361. }
  362.  
  363. bool UserDefinedFieldType::ccheck(int c, const void *u) {
  364.   NCursesFormField* F = (NCursesFormField*)u;
  365.   assert(F != 0);
  366.   UserDefinedFieldType* udf =
  367.     (UserDefinedFieldType*)(F->fieldtype());
  368.   assert(udf != 0);
  369.   return udf->char_check(c);
  370. }
  371.  
  372. void* UserDefinedFieldType::makearg(va_list* va) {
  373.   return va_arg(*va,NCursesFormField*);
  374. }
  375.  
  376. FIELDTYPE* UserDefinedFieldType::generic_fieldtype =
  377.   ::new_fieldtype(UserDefinedFieldType::fcheck,
  378.           UserDefinedFieldType::ccheck);
  379.  
  380. FIELDTYPE* UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice =
  381.   ::new_fieldtype(UserDefinedFieldType::fcheck,
  382.           UserDefinedFieldType::ccheck);
  383.  
  384. bool UserDefinedFieldType_With_Choice::next_choice(FIELD *f, const void *u) {
  385.   NCursesFormField* F = (NCursesFormField*)u;
  386.   assert(F != 0);
  387.   UserDefinedFieldType_With_Choice* udf =
  388.     (UserDefinedFieldType_With_Choice*)(F->fieldtype());
  389.   assert(udf != 0);
  390.   return udf->next(*F);
  391. }
  392.  
  393. bool UserDefinedFieldType_With_Choice::prev_choice(FIELD *f, const void *u) {
  394.   NCursesFormField* F = (NCursesFormField*)u;
  395.   assert(F != 0);
  396.   UserDefinedFieldType_With_Choice* udf =
  397.     (UserDefinedFieldType_With_Choice*)(F->fieldtype());
  398.   assert(udf != 0);
  399.   return udf->previous(*F);
  400. }
  401.  
  402. class UDF_Init {
  403. private:
  404.   int code;
  405.   static UDF_Init* I;
  406. public:
  407.   UDF_Init() {
  408.     code = ::set_fieldtype_arg(UserDefinedFieldType::generic_fieldtype,
  409.                    UserDefinedFieldType::makearg,
  410.                    NULL,
  411.                    NULL);
  412.     if (code==E_OK)
  413.       code = ::set_fieldtype_arg
  414.     (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
  415.      UserDefinedFieldType::makearg,
  416.      NULL,
  417.      NULL);
  418.     if (code==E_OK)
  419.       code = ::set_fieldtype_choice
  420.     (UserDefinedFieldType_With_Choice::generic_fieldtype_with_choice,
  421.      UserDefinedFieldType_With_Choice::next_choice,
  422.      UserDefinedFieldType_With_Choice::prev_choice);
  423.   }
  424. };
  425.  
  426. UDF_Init* UDF_Init::I = new UDF_Init();
  427.  
  428.